<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	
	<xsl:template match="*"></xsl:template>
	
	<xsl:template match="content">
		
		<a href='/list/'>Список</a> |
		<a href="/show/{object/@uid}">Просмотр</a>
		
		<h3>Привет из шаблона контента действия EDIT</h3>

		<script type="text/javascript" charset="utf-8"><xsl:comment>
			
			function validate(sId, fValidator) {
				var jField = $("#" + sId);
				jField.keyup(function(e){
					if (fValidator(jField.val())) {
						jField.parent().parent().removeClass("required");
						jField.parent().removeClass("invalid");
					} else {
						jField.parent().parent().addClass("required");
						jField.parent().addClass("invalid");
					}
				});
				jField.keyup();
			}
			
			function multiplicate(sId) {
				var jContainer = $("#" + sId);
				jContainer.append('&lt;div class="repeat_control"&gt;&lt;a href="#" class="add_field"&gt;add&lt;/a&gt;&lt;/div&gt;');
				jContainer.find(".add_field").click(function(e){
					var jClone = jContainer.clone(true).insertAfter(jContainer);
					//var jCloneField = jClone.find("#" + sId);
					//jCloneField.attr("name", jCloneField.attr("name") + "[1]");
					//jCloneField.val("");
					return false;
				});
			}

		</xsl:comment></script>
		
		<form method="POST" enctype="multipart/form-data">
			<xsl:apply-templates select="object" />
		</form>
		
	</xsl:template>
	
	
	<!-- root object -->
	<xsl:template match="object">
		
		<xsl:apply-templates mode="field">
			<xsl:with-param name="name_prefix" select="concat(@class,'[',@uid,']')"/>
		</xsl:apply-templates>
			
		<input type="submit" value="Сохранить" />
	</xsl:template>

	
	<!-- тип по умолчанию -->
	<xsl:template match="*" mode="field">
		
		<xsl:param name="name_prefix" />
		<xsl:param name="name" select="concat($name_prefix,'.',name())" />
		
		<dl class="row for_input">
			<dt class="label">
				<label><xsl:value-of select="name()" /></label>
			</dt>
			<dd class="field">
				<input uid="{generate-id()}" type="text" name="{$name}" class="text" value="{text()}" />
			</dd>
		</dl>
				
		<script type="text/javascript" charset="utf-8"><xsl:comment>
			validate("<xsl:value-of select="generate-id()"/>", function(value){ return value.length > 5? true: false; });
		</xsl:comment></script>
		
	</xsl:template>
	
	
	<!-- поле типа текст -->
	<xsl:template match="*[@type='text']" mode="field">
		
		<xsl:param name="name_prefix" />
		<xsl:param name="name" select="concat($name_prefix,'.',name())" />
		<xsl:param name="uid" select="generate-id()"/>
		
		<dl class="row for_input">
			<dt class="label">
				<label><xsl:value-of select="name()" /></label>
			</dt>
			<dd class="field">
				<textarea uid="{$uid}" name="{$name}"><xsl:value-of select="text()" /></textarea>
			</dd>
		</dl>
		
	</xsl:template>
	
	
	<!-- один объект -->
	<xsl:template match="*[@class]" mode="field">
		
		<xsl:param name="name_prefix" />
		<xsl:param name="part_of_many" />
		
		<fieldset>
			<legend>Объект класса <xsl:value-of select="@class" /></legend>
				<xsl:choose>
					<xsl:when test="$part_of_many">
						<xsl:apply-templates mode="field">
							<xsl:with-param name="name_prefix" select="concat($name_prefix,'[',@uid,']')"/>
						</xsl:apply-templates>
					</xsl:when>
					<xsl:otherwise>
						<xsl:apply-templates mode="field">
							<xsl:with-param name="name_prefix" select="concat($name_prefix,'.',name())"/>
						</xsl:apply-templates>
					</xsl:otherwise>
				</xsl:choose>
		</fieldset>
	</xsl:template>
	
	
	<!-- много объектов -->
	<xsl:template match="*[@link_type='has_many']" mode="field">
		
		<xsl:param name="name_prefix" />

		<fieldset>
			<legend>Объекты класса <xsl:value-of select="@linked_class" /></legend>
			<xsl:apply-templates mode="field">
				<xsl:with-param name="name_prefix" select="concat($name_prefix,'.',name())"/>
				<xsl:with-param name="part_of_many" />
			</xsl:apply-templates>
			<div name="pictures">
				<fieldset>
					<legend>Новая картинка</legend>
					<input uid="{generate-id()}_alt" name="{$name_prefix}.oImage[new_1].alt" type="text" value="" />
					<input uid="{generate-id()}_file_name" name="{$name_prefix}.oImage[new_1].oFile.name" type="file" value="" />
					<label><input type="checkbox" name="{$name_prefix}.oImage[new_1].remove" uid="remove" />Удалить</label>
				</fieldset>
			</div>
		</fieldset>
	</xsl:template>
	

	<!-- дочерние объекты класса Picture -->
	<xsl:template match="*[@class='Picture']" mode="field">
		
		<xsl:param name="name_prefix" />
		<xsl:param name="name" select="concat($name_prefix,'[',@id,']')" />
		
		<div class="pictures" uid="{generate-id()}">
			<fieldset>
				<legend>Картинка</legend>
				<img src="/images/calendars/{oFile/name/text()}" alt="{alt/text()}" height="170" />
				<input uid="{generate-id(alt)}" name="{$name}.alt" type="text" value="{alt/text()}" />
				<input uid="{generate-id(oFile/name)}" name="{$name}.oFile.name" type="file" value="{oFile/name/text()}" />
				<label><input type="checkbox" name="{$name}.remove" />Удалить</label>
			</fieldset>
			
			<script type="text/javascript" charset="utf-8"><xsl:comment>
				multiplicate("<xsl:value-of select="generate-id()"/>");
			</xsl:comment></script>
		</div>
				
	</xsl:template>

</xsl:stylesheet>